package com.sun.wms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sun.wms.entity.User;
import com.sun.wms.mapper.UserMapper;
import com.sun.wms.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public List<User> selectAll() {
        return userMapper.selectList(null);
    }
    @Override
    public List<User> queryUsers(User user) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if (user.getName() != null) {
            queryWrapper.eq("name", user.getName());
        }
        if (user.getAge() != null) {
            queryWrapper.eq("age", user.getAge());
        }
        return userMapper.selectList(queryWrapper);
    }



    @Override
    public int updateUser(User user) {

            int rows = userMapper.updateById(user);
            return rows;


    }

    @Override
    public int deleteUsersBatch(List<Long> ids) {
        int rows = userMapper.deleteBatchIds(ids);
        return rows;
    }

    @Override
    public boolean addUsersBatch(List<User> users) {
        for (User user : users) {
            int rows = userMapper.insert(user);
            if (rows <= 0) {
                return false;
            }
        }
        return true;
    }
    }


